import 'package:dio/dio.dart';
import 'package:fsharing/core/models/business_condition.dart';
import 'package:fsharing/core/models/business_level.dart';
import 'package:fsharing/core/services/api.dart';
import 'package:fsharing/core/viewmodels/view_model.dart';

class BusinessConditionModel extends ViewModel {
  final Api api;
  final BusinessLevel businessLevel;

  String _errorMessage;
  BusinessCondition _businessCondition;

  BusinessConditionModel(this.api, this.businessLevel) : super() {
    _initDate();
  }

  String get errorMessage => _errorMessage;

  BusinessCondition get businessCondition => _businessCondition;

  Future<ViewState> _initDate() async {
    viewState = ViewState.busy;
    try {
      _businessCondition =
          await api.businessApplyCondition(businessLevel.businessType, businessLevel.businessLevel);
      viewState = ViewState.success;
    } catch (e) {
      _errorMessage = e is DioError ? e.message : e.toString();
      viewState = ViewState.error;
    }
    return viewState;
  }
}
